{# SPDX-License-Identifier: Apache-2.0 -#}

{% extends "admin/base.html" %}

{% import "admin/utils/pagination.html" as pagination %}

{% set perms_admin_prohibited_email_domain_write = request.has_permission(Permissions.AdminProhibitedEmailDomainsWrite) %}

{% block title %}
  Prohibited Email Domains
{% endblock title %}

{% block breadcrumb %}
  <li class="breadcrumb-item">Prohibition Era</li>
  <li class="breadcrumb-item active">Email Domains</li>
{% endblock breadcrumb %}

{% block content %}
  <div class="card">
    <div class="card-body">
      <form>
        <div class="input-group input-group-lg">
          <label for="search" title="Search"></label>
          <input name="q"
                 id="search"
                 type="text"
                 class="form-control input-lg"
                 placeholder="Search"
                 {% if query %}value="{{ query }}"{% endif %}>
          <div class="input-group-btn input-group-append">
            <button type="submit" class="btn btn-default">
              <i class="fa fa-search"></i>
            </button>
          </div>
        </div>
      </form>
    </div>
  </div>
  <div class="card">
    <div class="card-body table-responsive p-0">
      <table class="table table-hover table-striped">
        <thead>
          <tr>
            <th>Domain Name</th>
            <th>MX record?</th>
            <th>Prohibited by</th>
            <th>Prohibited on</th>
            <th>Comment</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          {% for prohibited_email_domain in prohibited_email_domains %}
            <tr>
              <td>{{ prohibited_email_domain.domain }}</td>
              <td>
                {% if prohibited_email_domain.is_mx_record %}<i class="fa fa-check text-success"></i>{% endif %}
              </td>
              <td>
                <a href="{{ request.route_path('admin.user.detail', username=prohibited_email_domain.prohibited_by.username) }}">
                  {{ prohibited_email_domain.prohibited_by.username }}
                </a>
              </td>
              <td>{{ prohibited_email_domain.created | format_datetime }}</td>
              <td>{{ prohibited_email_domain.comment }}</td>
              <td>
                <a href="{{ request.route_path('admin.user.list', _query={'q': 'email:%@' + prohibited_email_domain.domain}) }}"
                   class="btn btn-primary btn-sm"
                   title="Search for users with this domain">
                  <i class="fa-solid fa-users"></i>
                </a>
                <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#removeEmailModal-{{ prohibited_email_domain.id }}" {{ "disabled" if not perms_admin_prohibited_email_domain_write }}>
                  <i class="fa fa-trash"></i>
                </button>
                <div class="modal fade"
                     id="removeEmailModal-{{ prohibited_email_domain.id }}"
                     tabindex="-1"
                     role="dialog">
                  <form method="post"
                        action="{{ request.route_path('admin.prohibited_email_domains.remove', id=prohibited_email_domain.id) }}">
                    <input name="csrf_token"
                           type="hidden"
                           value="{{ request.session.get_csrf_token() }}">
                    <div class="modal-dialog" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h4 class="modal-title" id="removeModalLabel">Remove prohibited email domain {{ prohibited_email_domain.domain }}?</h4>
                          <button type="button" class="close" data-dismiss="modal">
                            <span>×</span>
                          </button>
                        </div>
                        <div class="modal-body">
                          <p>
                            Remove the prohibited email domain <strong>{{ prohibited_email_domain.domain }}</strong>?
                          </p>
                          <p>
                            Once removed, new registrations with this domain will be allowed,
                            unless the domain also exists in the
                            <a href="https://github.com/disposable-email-domains/disposable-email-domains"
                               target="_blank">
                              <code>disposable-email-domains</code>
                            </a>
                            list.
                          </p>
                          <hr>
                          <p>
                            To remove the prohibited email domain, please enter the domain name <strong>{{ prohibited_email_domain.domain }}</strong> below.
                          </p>
                          <label>
                            <input type="text"
                                   name="domain_name"
                                   placeholder="{{ prohibited_email_domain.domain }}"
                                   required>
                          </label>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                          <button type="submit" class="btn btn-danger">Delete</button>
                        </div>
                      </div>
                    </div>
                  </form>
                </div>
              </td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    <div class="card-footer">
      <div class="col-sm-5">{{ pagination.summary(prohibited_email_domains) }}</div>
      <div class="col-sm-7">
        <div class="float-right">{{ pagination.paginate(prohibited_email_domains) }}</div>
      </div>
    </div>
  </div>
  <div class="col-6">
    <div class="card card-primary">
      <div class="card-header">
        <h3 class="card-title">Prohibit email domain</h3>
      </div>
      <form method="post"
            action="{{ request.route_path('admin.prohibited_email_domains.add') }}">
        <input name="csrf_token"
               type="hidden"
               value="{{ request.session.get_csrf_token() }}">
        <div class="card-body">
          <div class="form-group">
            <label for="prohibitedEmailDomain">Email domain</label>
            <input name="email_domain" class="form-control" id="prohibitedEmailDomain" placeholder="Enter email domain name to prohibit " {{ "disabled" if not perms_admin_prohibited_email_domain_write }} autocomplete="off" autocapitalize="off" required>
          </div>
          <div class="form-check">
            <input name="is_mx_record"
                   type="checkbox"
                   class="form-check-input"
                   id="mxDomain">
            <label class="form-check-label" for="mxDomain">Prohibit all domains that use this domain as an MX record?</label>
          </div>
          <div class="form-group">
            <label for="prohibitedComment">Comment</label>
            <textarea name="comment" class="form-control" id="prohibitedComment" rows="3" placeholder="Enter comment ..." {{ "disabled" if not perms_admin_prohibited_email_domain_write }}></textarea>
          </div>
        </div>
        <div class="card-footer">
          <button type="submit" class="btn btn-primary" title="{{ 'Submitting requires superuser privileges' if not perms_admin_prohibited_email_domain_write }}" {{ "disabled" if not perms_admin_prohibited_email_domain_write }}>Submit
          </button>
        </div>
      </form>
    </div>
  </div>
{% endblock content %}
